//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;
using Microsoft.BizTalk.ApplicationDeployment;

namespace GML.BizTalk.OperationManager.Library
{
    public class OrphanOrchestrationManagement
    {
        public static bool RemoveOrchestration(GML.BizTalk.OperationManager.MSBTSOperations.Orchestration[] orchs, string btsDbServerName, string btsMgntDbName)
        {
            string str3 = btsDbServerName;
            string str4 = btsMgntDbName;
            Group group = new Group();
            group.DBServer = str3;
            group.DBName = str4;
            Microsoft.BizTalk.ApplicationDeployment.Application application = null;
            try
            {
                GML.BizTalk.OperationManager.MSBTSOperations.Orchestration[] removalOrchs = GetRemovalList(orchs, btsDbServerName, btsMgntDbName);
                if (removalOrchs == null) return true;
                foreach (GML.BizTalk.OperationManager.MSBTSOperations.Orchestration orch in removalOrchs)
                {
                    string str = orch.ApplicationName; // "GML.WorkFlow";
                    string luid = orch.BtsAssembly.DisplayName; //base.Args["Luid"];
                    application = group.Applications[str];
                    try
                    {
                        if (application.IsSystem)
                        {
                            throw new ArgumentException("Cannot Remove System Application.");
                            //throw new ArgumentException(CommandResources.GetString(CommandResources.ResourceID.CannotRemoveResourceFromSystemApp));
                        }
                        application.UILevel = 2;
                        //application.Log += new LogEventHandler(this, (IntPtr)this.OnLog);
                        application.ResourceCollection.Remove("", luid);

                        return true;
                    }
                    catch (Exception exception)
                    {
                        System.Diagnostics.Debug.WriteLine(exception.ToString());
                        group.Abort();
                        throw;
                    }
                    finally
                    {
                        if (group != null)
                        {
                            group.Dispose();
                        }
                    }

                }

            }
            catch (Exception)
            {
                
                throw;
            } return true;

        }

        private static GML.BizTalk.OperationManager.MSBTSOperations.Orchestration[] GetRemovalList
            (GML.BizTalk.OperationManager.MSBTSOperations.Orchestration[] excludeOrchs, string btsDbServerName, string btsMgntDbName)
        {
            List<GML.BizTalk.OperationManager.MSBTSOperations.Orchestration> unenlistedOrchs = null;
            List<GML.BizTalk.OperationManager.MSBTSOperations.Orchestration> removalOrchs = new List<GML.BizTalk.OperationManager.MSBTSOperations.Orchestration>();
            string btsAppName = excludeOrchs[0].ApplicationName;
            bool isRemoval = true;
            try
            {
                unenlistedOrchs = GML.BizTalk.OperationManager.Library.OrchestrationStateManagement.GetUnenlistedOrchestrations(btsAppName,btsDbServerName, btsMgntDbName);
                foreach (GML.BizTalk.OperationManager.MSBTSOperations.Orchestration unenlistedOrch in unenlistedOrchs)
                {
                    isRemoval = true;
                    foreach (GML.BizTalk.OperationManager.MSBTSOperations.Orchestration orch in excludeOrchs)
                    {
                        if (orch.Fullname.Equals(unenlistedOrch.Fullname) && orch.BtsAssembly.DisplayName.Equals(unenlistedOrch.BtsAssembly.DisplayName))
                        {
                            isRemoval = false;
                            break;
                        }
                    }
                    if (isRemoval)
                    {
                        removalOrchs.Add(unenlistedOrch);
                    }
                    //else
                    //{
                    //    isRemoval = false;
                    //}
                }
                return removalOrchs == null ? null : removalOrchs.ToArray();
            }
            catch (Exception ex)
            {
                Logger.doLog(Guid.NewGuid().ToString(), ex.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), ex.StackTrace, Logger.ServerityLevel.ERROR);
                throw ex;
            }
        }

    }
}
